package cc.miwuya.property.controller;

import cc.miwuya.common.util.Result;
import cc.miwuya.core.BaseController;
import cc.miwuya.property.domain.Meter;
import cc.miwuya.property.service.MeterService;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Arrays;

/**
 * @Author 迷雾森林
 * @Date 2024-02-27 11:59
 * @Desc 计量表控制器
 */
@RestController
@RequestMapping("/property/meter")
public class MeterController extends BaseController {

    @Resource
    MeterService meterService;

    /**
     * 添加或修改计量表
     */
    @PostMapping("/edit")
    public Result edit(@RequestBody Meter meter) {
        meterService.saveOrUpdate(meter);
        return Result.success();
    }

    @GetMapping("/info/{meterId}")
    public Result info(@PathVariable Long meterId) {
        Meter meter = meterService.getById(meterId);
        return Result.success(meter);
    }

    @GetMapping("/delete/{meterId}")
    public Result delete(@PathVariable Long meterId) {
        meterService.removeById(meterId);
        return Result.success();
    }

    @GetMapping("/getHouseMeter/{houseId}")
    public Result getHouseMeterList(@PathVariable Long houseId) {
        return Result.success(meterService.list(new QueryWrapper<Meter>().eq("home_id", houseId)));
    }

    /**
     * 获取小区所有计量表
     */
    @GetMapping("/residentialList")
    public Result residentialList(Long residentialId,Integer meterType) {
        return Result.success(meterService.getResidentialList(getPage(),residentialId,meterType));
    }

    /**
     * 为房屋初始化计量表
     */
    @GetMapping("/addHouseMeter")
    public Result addHouseMeter(Long houseId, Long residentialId) {
        Meter waterMeter = new Meter();
        waterMeter.setHomeId(houseId);
        waterMeter.setResidentialId(residentialId);
        waterMeter.setType(1);
        Meter gasMeter = new Meter();
        gasMeter.setHomeId(houseId);
        gasMeter.setResidentialId(residentialId);
        gasMeter.setType(2);
        Meter electricMeter = new Meter();
        electricMeter.setHomeId(houseId);
        electricMeter.setResidentialId(residentialId);
        electricMeter.setType(3);
        return Result.success(meterService.saveBatch(Arrays.asList(waterMeter, gasMeter, electricMeter)));
    }
    @GetMapping("/getOwnerMeterList")
    public Result getOwnerMeterList(Long ownerId,Integer meterType) {
        return Result.success(meterService.getOwnerMeterList(ownerId,meterType));
    }
}
